package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.common.exception.OrderReservationsException;
import com.atguigu.common.utils.DateUtils;
import com.atguigu.dao.OrderDao;
import com.atguigu.dao.UserDao;
import com.atguigu.pojo.Member;
import com.atguigu.pojo.Order;
import com.atguigu.pojo.OrderSetting;
import com.atguigu.service.MemberService;
import com.atguigu.service.OrderInfoService;
import com.atguigu.service.OrderSettingService;
import com.atguigu.vo.HotSetmealVo;
import com.atguigu.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service(interfaceClass = OrderInfoService.class)
@Transactional
public class OrderInfoServiceImpl implements OrderInfoService {

    @Autowired
    MemberService memberService;
    @Autowired
    OrderDao orderDao;
    @Autowired
    OrderSettingService orderSettingService;
    @Autowired
    UserDao userDao;

    @Override
    public Order submitReservation(OrderVo orderVo) throws Exception {

        //判断日期是否可以预约
        OrderSetting orderSetting = orderSettingService.selectOrderDate(DateUtils.parseDate2String(orderVo.getOrderDate()));
        if (orderSetting==null){
            throw new OrderReservationsException("该日期不可预约");
        }
        //根据日期插叙预约信息 判断是否预约已满

        //1.根据身份证号查询
        Member member = memberService.selectIdCard(orderVo.getIdCard());
         //1)查询不到则添加用户信息
        if (member==null){
            member = new Member();
            member.setIdCard(orderVo.getIdCard());
            member.setSex(orderVo.getSex().toString());
            member.setPhoneNumber(orderVo.getTelephone());
            member.setRegTime(new Date());
            member.setName(orderVo.getName());
            memberService.insert(member);
        }
        //2)查询成功获取用户id 判断是否进行过一次预约预约信息
        Order order = new Order(member.getId(),orderVo.getOrderDate(), Order.ORDERTYPE_WEIXIN,Order.ORDERSTATUS_NO,orderVo.getSetmealId());
        Order order1 = orderDao.selectOne(order);
        if (order1!=null){
            throw new OrderReservationsException("您已经预约过一次 不可重复预约");
        }
        //2.更改当天的预约数量
        int insert = orderSettingService.updateReservations(orderVo.getOrderDate());
        if (insert<=0){
            //预约已满
            throw new OrderReservationsException("预约已满");
        }
        //添加预约
        orderDao.insert(order);
        return order;
    }

    /*
    查询所有套餐名称 预约数量 占比
     */
    @Override
    public List<HotSetmealVo> selectHotSetmeal() {
        return orderDao.selectHotSetmeal();
    }

    @Override
    public Integer selectThisMonthVisitsNumber(String MonthString) {
        return orderDao.selectThisMonthVisitsNumber(MonthString);
    }

    @Override
    public Integer selectThisMonthOrderNumber(String MonthString) {
        return orderDao.selectThisMonthOrderNumber(MonthString);
    }

    @Override
    public Integer selectThisWeekVisitsNumber(Date weekMonday, Date lastDay) {
        return orderDao.selectThisWeekVisitsNumber(weekMonday,lastDay);
    }

    @Override
    public Integer selectThisWeekOrderNumber(Date weekMonday, Date lastDay) {
        return orderDao.selectThisWeekOrderNumber(weekMonday,lastDay);
    }

    @Override
    public Integer selectOrderType(Date day) {
        return orderDao.selectDay(day);
    }

    @Override
    public Integer selectTodayVisitsNumber(Date day) {
        return orderDao.selectTodayVisitsNumber(day);
    }

    @Override
    public Order findById(Integer id) {
        return orderDao.findById(id);
    }
}
